<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<!-- Casting SPELs in Lisp - Emacs Lisp Edition, a Comic Book
     Written by Conrad Barski, M.D., http://lisperati.com
     Edited by James A. Webb, http://uberkode.com -->

<head>

  <title>Casting SPELs in Lisp (21)</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="author" content="James A. Webb" />
  <meta name="date" content="2007-09-16T00:00:00+00:00" />
  <meta name="keywords" content="Emacs, Lisp" />

</head>

<body>

<center>

<table cellspacing="10" width="500">
  <tr>
    <td bgcolor="#AAFFAA" height="50" align="center">
        <b>Walking Around in Our World</b>
    </td>
  </tr>
  <tr>
    <td>
      Ok, now that we can see our world, let's write some code that
      lets us walk around in it. The function <tt>walk-direction</tt> 
      (not in the <i>Functional</i> style) takes a direction and lets 
      us walk there:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (defun walk-direction (direction)
    (let ((next (assoc direction (cddr (assoc location map)))))
      (cond (next (setf location (third next)) (look))
	    (t '(you cannot go that way -)))))
</pre>
    </td>
  </tr>
  <tr>
    <td>
      The special command <tt>let</tt> allows us to create the local 
      variable <tt>next</tt>, which we set to the path descriptor for 
      the direction the player wants to walk in - <tt>cdr</tt> just 
      chops the first item off of a list. If the user types in a 
      bogus direction, <tt>next</tt> will be <tt>nil</tt>. The 
      <tt>cond</tt> command is like a chain of if-then commands in
      Lisp: Each row in a <tt>cond</tt> has a value to check and an 
      action to do. In this case, if the next location is not 
      <tt>nil</tt>, it will <tt>setf</tt> the player's location to 
      the third item in the path descriptor, which holds the symbol 
      describing the new direction, then gives the user a look of 
      the new place. If the next location is <tt>nil</tt>, it falls 
      through to the next line and admonishes the user. Let's try it:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (walk-direction 'west)
</pre>
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkgreen">
  ==> (you are in a beautiful garden - 
       there is a well in front of you - 
       there is a door going east from here -
       you see a frog on the floor -
       you see a chain on the floor -)
</pre>
    </td>
  </tr>
  <tr>
    <td>
      Now, we were able to simplify our description functions by
      creating a <tt>look</tt> command that is easy for our player to 
      type. Similarly, it would be nice to adjust the 
      <tt>walk-direction</tt> command so that it doesn't have an 
      annoying quote mark in the command that the player has to type 
      in. But, as we have learned, when the interpreter reads a form 
      in <i>Code</i> mode, it will read all its parameters in 
      <i>Code</i> mode, unless a quote tells it not to. Is there 
      anything we can do to tell the interpreter that <tt>west</tt>
      is just a piece of data without the quote?
    </td>
  </tr>
  <tr>
    <td align="center">
      &lt;&lt; <a href="casting-spels-emacs-1.html">begin</a>
      &lt; <a href="casting-spels-emacs-20.html">previous</a> - 
      <a href="casting-spels-emacs-22.html">next</a> &gt;
      <a href="casting-spels-emacs-35.html" >end</a> &gt;&gt;
    </td>
  </tr>
</table>

</center>

</body>

</html>